/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.client;

import org.testis.communication.client.AtofFtpClient;
import org.testis.utils.TerminalApplicationExecutor;
import java.io.IOException;
import java.net.SocketException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author nurhat
 */
public class TestFtp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws SocketException, IOException {
        AtofFtpClient ftp1 = new AtofFtpClient("172.16.9.168", "ftp", "ftp");

        if (!ftp1.connectTo()) {
            return;
        }
        if (!ftp1.loginTo()) {
            return;
        }

        String maxRevisionFolder = "", maxVersionFolder = "", maxVersion = "", maxRevision = "";
        String ATOF_CLIENT_HOME = System.getenv("ATOF_CLIENT_HOME");
        String ATOF_CLIENT_UPDATE_HOME = ATOF_CLIENT_HOME + "/../Updates/";

        try {
            String mainPath = "/ATOFSETUPS/";
            String[] versionNames = ftp1.listNames(mainPath);
            if (versionNames.length > 0) {
                Arrays.sort(versionNames);
                maxVersionFolder = versionNames[versionNames.length - 1];
                maxVersion = versionNames[versionNames.length - 1].replace(mainPath, "");
                for (String name : versionNames) {
                    System.out.println("Name = " + name);
                }
                String[] revisionNames = ftp1.listNames(maxVersionFolder);
                if (revisionNames.length > 0) {

                    Arrays.sort(revisionNames);
                    maxRevisionFolder = revisionNames[revisionNames.length - 1];
                    maxRevision = revisionNames[revisionNames.length - 1].replace(maxVersionFolder, "");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } finally {
            ftp1.logoutFrom();

        }
        if (maxVersion.isEmpty() || maxVersion.isEmpty()) {
            System.out.println("Can not get new version info");
            return;
        }
        String app = "D:\\Airties\\AtofClient\\vendor\\wget -r -q  ftp://ftp:ftp@172.16.9.168" + maxRevisionFolder + " -P " + ATOF_CLIENT_UPDATE_HOME;
        System.out.println(app);
        TerminalApplicationExecutor executor = new TerminalApplicationExecutor(app);
        try {
            executor.runExec();
        } catch (InterruptedException ex) {
            System.out.println(ex.getMessage());
        }


    }
}
